/*
 * @Author: 张笑天
 * @Date: 2018-03-07 17:11:25
 * @Last Modified by: Theoton
 * @Last Modified time: 2018-07-04 16:00:27
 */
import { observable, action } from 'mobx';
import { message } from 'antd';
import { WeaLocaleProvider } from 'ecCom';

import Api from '../apis/ops';

const getLabel = WeaLocaleProvider.getLabel;
const initState = {
  loading: false,
  emailSize: {
    occupyspace: 0,
    totalspace: 100,
    remainspace: 0,
    userid: '',
  },
};

export default class EmailOpsStore {
  @observable loading = initState.loading;
  @observable emailSize = initState.emailSize;
  @observable spaceRight = 0;

  @action
  setState = (params = {}) => {
    Object.keys(params).forEach((key) => {
      this[key] = params[key];
    });
    return this;
  }

  @action
  getEmailSize = async (params) => {
    this.loading = true;
    const res = await Api.getEmailSize(params);
    try {
      if (res.status === '1') {
        const { occupyspace, totalspace, spaceRight, remainspace, userid } = res;
        this.emailSize = { occupyspace, totalspace, remainspace, userid };
        this.spaceRight = spaceRight;
      } else {
        message.error(res.msg);
      }
    } catch (e) {
      message.error(e);
    }
    this.loading = false;
    return res;
  }

  @action
  setStar = async (params) => {
    this.loading = true;
    const res = await Api.setStar(params);
    try {
      if (res.status === '1') {
        const { star } = params;
        message.success(`${star === 1 ? getLabel(382626, '标星') : getLabel(201, '取消')}${getLabel(84565, '成功！')}`);
      } else {
        message.error(res.msg || getLabel(83912, '操作失败！'));
      }
    } catch (e) {
      message.error(e);
    }
    this.loading = false;
    return res;
  }

  @action
  setReadStatus = async (params, noMessage) => {
    this.loading = true;
    const res = await Api.setReadStatus(params);
    try {
      if (res.status === '1') {
        const { status } = params;
        !noMessage && message.success(`${status === 1 ? getLabel(18492, '标记为已读') : getLabel(25420, '标记为未读')}${getLabel(84565, '成功！')}`);
      } else {
        message.error(res.msg || getLabel(83912, '操作失败！'));
      }
    } catch (e) {
      message.error(e);
    }
    this.loading = false;
    return res;
  }

  @action
  deleteEmail = async (params) => {
    this.loading = true;
    const res = await Api.deleteEmail(params);
    try {
      if (res.status === '1') {
        const { operate } = params;
        message.success(`${operate === 'delete' ? getLabel(91, '删除') : getLabel(2031, '永久删除')}${getLabel(84565, '成功！')}`);
      } else {
        message.error(res.msg || getLabel(83473, '删除失败！'));
      }
    } catch (e) {
      message.error(e);
    }
    this.loading = false;
    return res;
  }

  @action
  moveToFolder = async (params) => {
    this.loading = true;
    const res = await Api.moveToFolder(params);
    try {
      if (res.status === '1') {
        message.success(getLabel(384404, '移动成功！'));
      } else {
        message.error(res.msg || getLabel(384405, '移动失败！'));
      }
    } catch (e) {
      message.error(e);
    }
    this.loading = false;
    return res;
  }

  @action
  setTag = async (params) => {
    this.loading = true;
    const res = await Api.setTag(params);
    try {
      if (res.status === '1') {
        const { operation } = params;
        message.success(`${operation === 'cancelLabel' || operation === 'removeLabel'
          ? getLabel(201, '取消') : getLabel(611, '添加')}${getLabel(384406, '标签成功！')}`);
      } else {
        message.error(res.msg || getLabel(384407, '设置标签失败！'));
      }
    } catch (e) {
      message.error(e);
    }
    this.loading = false;
    return res;
  }

  @action
  recall = async (params) => {
    this.loading = true;
    const res = await Api.recall(params);
    try {
      if (res.status === '1' && res.flag === true) {
        message.success(getLabel(384408, '撤回成功！'));
      } else {
        message.error(getLabel(505893, '撤回失败！您暂无撤回权限，或者可撤回时间已过期，请检查后台邮件撤回相关配置。'));
      }
    } catch (e) {
      message.error(e);
    }
    this.loading = false;
    return res;
  }

  @action
  exportToDoc = async (params) => {
    this.loading = true;
    const res = await Api.exportToDoc(params);
    try {
      if (res.status === '1') {
        message.success(getLabel(384410, '导出文档成功！'));
      } else if (res.status === '2') {
        message.error(getLabel(390255, '该邮件已被删除！'));
      } else {
        message.error(res.msg || getLabel(384411, '导出文档失败！'));
      }
    } catch (e) {
      message.error(e);
    }
    this.loading = false;
    return res;
  }

  @action
  exportToCrm = async (params) => {
    this.loading = true;
    const res = await Api.exportToCrm(params);
    try {
      if (res.status === '1') {
        message.success(getLabel(384412, '导出客户联系成功！'));
      } else if (res.status === '2') {
    	  message.error(getLabel(390255, '该邮件已被删除！'));
      } else {
        message.error(res.msg || getLabel(384413, '导出客户联系失败！'));
      }
    } catch (e) {
      message.error(e);
    }
    this.loading = false;
    return res;
  }

  // @action
  // exportToCrm = (params) => {
  //   this.loading = true;
  //   Api.exportToCrm(params).then(res => {
  //     console.log('res', res)
  //   })
  // }

  @action
  exportToEml = async (params) => {
    this.loading = true;
    const res = await Api.exportToEml(params);
    try {
      if (res.status !== '1') {
        message.error(res.msg || getLabel(384414, '导出EML失败！'));
      }
    } catch (e) {
      message.error(e);
    }
    this.loading = false;
    return res;
  }

  @action
  exportToHtml = async (params) => {
    this.loading = true;
    const res = await Api.exportToHtml(params);
    try {
      if (res.status !== '1') {
        message.error(res.msg || '导出Html失败！');
      }
    } catch (e) {
      message.error(e);
    }
    this.loading = false;
    return res;
  }
}
